//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GaussHermiteIntegration.h"
using namespace Cephei::QL::Math::Integrals;
#include <gen/QL/Math/Integrals/GaussianOrthogonalPolynomial.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Integrals/GaussianQuadrature.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (UInt64 n, Microsoft::FSharp::Core::FSharpOption<Double>^ mu) : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
    try
    {
#ifdef HANDLE
        _phGaussHermiteIntegration = NULL;
#endif
        QuantLib::Size _n = (QuantLib::Size)ValueHelper::Convert (n); //d
        QuantLib::Real _mu = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (mu) ? (QuantLib::Real)ValueHelper::Convert (mu->Value) : 0.0); //4
        _ppGaussHermiteIntegration = new boost::shared_ptr<QuantLib::GaussHermiteIntegration> (new QuantLib::GaussHermiteIntegration ( _n,  _mu ));
        SetGaussianQuadrature (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (boost::shared_ptr<QuantLib::GaussHermiteIntegration>& childNative, Object^ owner) : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
#ifdef HANDLE
	_phGaussHermiteIntegration = NULL;
#endif
	_ppGaussHermiteIntegration = &childNative;
    _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
}
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (QuantLib::GaussHermiteIntegration& childNative, Object^ owner) : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
#ifdef HANDLE
	_phGaussHermiteIntegration = NULL;
#endif
	_ppGaussHermiteIntegration = new boost::shared_ptr<QuantLib::GaussHermiteIntegration> (&childNative);
    _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
    _GaussHermiteIntegrationOwner = owner;
    _GaussianQuadratureOwner = owner;
}

Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (CGaussHermiteIntegration^ copy) : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
#ifdef HANDLE
	_phGaussHermiteIntegration = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGaussHermiteIntegration = new boost::shared_ptr<QuantLib::GaussHermiteIntegration> (copy->GetShared());
        _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
    }
}
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (PLATFORM::Type^ t) : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
#ifdef HANDLE
	_phGaussHermiteIntegration = NULL;
#endif
	if (!t->IsSubclassOf(CGaussHermiteIntegration::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (QuantLib::Handle<QuantLib::GaussHermiteIntegration>& childNative, Object^ owner)  : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
	_phGaussHermiteIntegration = &childNative;
	_ppGaussHermiteIntegration = &static_cast<boost::shared_ptr<QuantLib::GaussHermiteIntegration>>(childNative.currentLink());
    _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
    _GaussHermiteIntegrationOwner = owner;
}
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (QuantLib::Handle<QuantLib::GaussHermiteIntegration> childNative)  : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
	_phGaussHermiteIntegration = &childNative;
	_ppGaussHermiteIntegration = &static_cast<boost::shared_ptr<QuantLib::GaussHermiteIntegration>>(childNative.currentLink());
    _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::CGaussHermiteIntegration (QuantLib::GaussHermiteIntegration childNative)  : CGaussianQuadrature(CGaussHermiteIntegration::typeid)
{
#ifdef HANDLE
	_phGaussHermiteIntegration = NULL;
#endif
	_ppGaussHermiteIntegration = new boost::shared_ptr<QuantLib::GaussHermiteIntegration> (new QuantLib::GaussHermiteIntegration (childNative));
    _ppGaussianQuadrature = new boost::shared_ptr<QuantLib::GaussianQuadrature> (boost::dynamic_pointer_cast<QuantLib::GaussianQuadrature> (*_ppGaussHermiteIntegration));
}
#endif

Cephei::QL::Math::Integrals::CGaussHermiteIntegration::~CGaussHermiteIntegration ()
{
    if (_ppGaussHermiteIntegration != NULL)
    {
	    delete _ppGaussHermiteIntegration;
        _ppGaussHermiteIntegration = NULL;
    }
}
Cephei::QL::Math::Integrals::CGaussHermiteIntegration::!CGaussHermiteIntegration ()
{
    if (_ppGaussHermiteIntegration != NULL)
    {
	    delete _ppGaussHermiteIntegration;
    }
}
QuantLib::GaussHermiteIntegration& Cephei::QL::Math::Integrals::CGaussHermiteIntegration::GetReference ()
{
    if (_ppGaussHermiteIntegration == NULL) throw REFNEW NativeNullException ();
	return **_ppGaussHermiteIntegration;
}
boost::shared_ptr<QuantLib::GaussHermiteIntegration>& Cephei::QL::Math::Integrals::CGaussHermiteIntegration::GetShared ()
{
    if (_ppGaussHermiteIntegration == NULL) throw REFNEW NativeNullException ();
	return *_ppGaussHermiteIntegration;
}
QuantLib::GaussHermiteIntegration* Cephei::QL::Math::Integrals::CGaussHermiteIntegration::GetPointer ()
{
    if (_ppGaussHermiteIntegration == NULL) throw REFNEW NativeNullException ();
	return &**_ppGaussHermiteIntegration;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GaussHermiteIntegration>& Cephei::QL::Math::Integrals::CGaussHermiteIntegration::GetHandle ()
{
	if (_phGaussHermiteIntegration == NULL)
	{
		_phGaussHermiteIntegration = new Handle<QuantLib::GaussHermiteIntegration> (*_ppGaussHermiteIntegration);
	}
	return *_phGaussHermiteIntegration;
}
#endif
bool Cephei::QL::Math::Integrals::CGaussHermiteIntegration::HasNative () 
{
	return (_ppGaussHermiteIntegration != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Integrals::IGaussHermiteIntegration^ Cephei::QL::Math::Integrals::CGaussHermiteIntegration_Factory::Create (UInt64 n, Microsoft::FSharp::Core::FSharpOption<Double>^ mu)
{
    return REFNEW CGaussHermiteIntegration ( n,  mu);
}
